﻿<%@ 
	Page
	Title="Home Page" 
	Language="C#" 
	MasterPageFile="~/Site.master" 
	AutoEventWireup="true"
	CodeBehind="Edit.aspx.cs" 
	Inherits="EyeOpen.CascadingDropDown.Web.Edit" 
%>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
	<asp:SqlDataSource 
		Id="customerSqlDataSource"
		runat="server" 
		ConnectionString="<%$ ConnectionStrings:FooConnectionString %>"
		SelectCommand="SELECT Customer.Id, Customer.Name, City.Name AS City, Town.Name AS Town FROM Customer INNER JOIN Town ON Customer.TownId = Town.Id INNER JOIN City ON Town.CityId = City.Id WHERE (Customer.Id = @Id) ORDER BY Customer.Name">
		<SelectParameters>
			<asp:QueryStringParameter 
				QueryStringField="id"
				Name="id" 
				Type="String" 
			/>
		</SelectParameters>
	</asp:SqlDataSource>
	<asp:HiddenField ID="customerHiddenField" runat="server" />
	<span class="fieldName">Name</span>
	<asp:TextBox ID="customerTextBox" runat="server" />

	<asp:SqlDataSource 
		Id="citySqlDataSource"
		runat="server" 
		ConnectionString="<%$ ConnectionStrings:FooConnectionString %>"
		SelectCommand="select [Id], [Name] FROM [City] ORDER BY [Name]">
	</asp:SqlDataSource>
	<div>
		<span class="fieldName">City</span>
		<asp:DropDownList
			Id="cityDropDownList" 
			runat="server" 
			DataSourceID="citySqlDataSource" 
			DataTextField="Name" 
			DataValueField="Id"
			AutoPostBack="True"
		/>
	</div>

	<asp:SqlDataSource 
		ID="townSqlDataSource" 
		runat="server" 
		ConnectionString="<%$ ConnectionStrings:FooConnectionString %>" 
		SelectCommand="SELECT [Id], [Name] FROM [Town] WHERE ([CityId] = @CityId) ORDER BY [Name]">
		<SelectParameters>
			<asp:ControlParameter 
				ControlID="cityDropDownList" 
				Name="CityId" 
				PropertyName="SelectedValue" 
				Type="String" 
			/>
		</SelectParameters>
	</asp:SqlDataSource>
	<div>
		<span class="fieldName">Town</span>
		<asp:DropDownList 
			ID="townDropDownList" 
			runat="server" 
			DataSourceID="townSqlDataSource" 
			DataTextField="Name" 
			DataValueField="Id">
		</asp:DropDownList>
	</div>

	<asp:SqlDataSource 
		Id="updateCustomerSqlDataSource"
		runat="server" 
		ConnectionString="<%$ ConnectionStrings:FooConnectionString %>"
		UpdateCommand="UPDATE [Customer] SET [Name] = @Name, [TownId] = @TownId WHERE ([Id] = @Id)">
		<UpdateParameters>
			<asp:ControlParameter 
				ControlID="customerHiddenField"
				Name="Id" 
				Type="String"
				PropertyName="Value"
			/>
			<asp:ControlParameter 
				ControlID="customerTextBox"
				Name="Name" 
				Type="String" 
				PropertyName="Text"
			/>
			<asp:ControlParameter 
				ControlID="townDropDownList"
				Name="TownId"
				PropertyName="SelectedValue"
				Type="String" 
			/>
		</UpdateParameters>
	</asp:SqlDataSource>
	<asp:LinkButton runat="server" Text="Save" OnClick="Save" />
</asp:Content>